﻿using Microsoft.EntityFrameworkCore;
using PesticideManage.API.Data;
using PesticideManage.Domain.AggregatesModel.PesticideAggregate;
using Shang.Microservice.Domain.Abstructions.SeedWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PesticideManage.API.Infrastructure.Repositories;
public class PesticideRepository : IPesticideRepository
{
    private readonly PesticideContext _context;
    public IUnitOfWork UnitOfWork => _context;
    public PesticideRepository(PesticideContext context)
    {
        this._context = context;
    }
    public Pesticide Add(Pesticide pesticide)
    {
        return _context.Pesticides.Add(pesticide).Entity;
    }

    public async Task<Pesticide> GetAsync(int pesticideId)
    {
        var order = await _context
                            .Pesticides
                            .FirstOrDefaultAsync(o => o.Id == pesticideId);
        if (order == null)
        {
            order = _context
                        .Pesticides
                        .Local
                        .FirstOrDefault(o => o.Id == pesticideId);
        }
        //if (order != null)
        //{
        //    await _context.Entry(order)
        //        .Collection(i => i.OrderItems).LoadAsync();
        //    await _context.Entry(order)
        //        .Reference(i => i.OrderStatus).LoadAsync();
        //}

        return order!;
    }

    public void Update(Pesticide pesticide)
    {
        _context.Entry(pesticide).State = EntityState.Modified;
    }
}
